package com.coe3.datahandler;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ParserXMLHandler extends DefaultHandler {
	
	public class ElementData {
		public String name;
		public final Map<String, String> attributes;
		public String value;
		public ElementData() {
			name = null;
			attributes = new HashMap<String, String>();
			value = null;
		}
	}

	public static final int START_DOCUMENT = 0;
	public static final int DID_START_ELEMENT = 1;
	public static final int DID_FINISH_ELEMENT = 8;
	public static final int	END_DOCUMENT = 10;

	private Boolean currentElement = false;
	private String currentValue = null;
	
//	private BaseActivity mActivity;
	private Handler handler;
	
	public ParserXMLHandler() {
        this( null, new Handler() );
	}
	
	public ParserXMLHandler( Handler _handler) {
//    	mActivity = baseActivity;
    	handler = _handler;
    }

    public ParserXMLHandler( BaseActivity baseActivity, Handler _handler) {
//    	mActivity = baseActivity;
    	handler = _handler;
    }
	
	@Override
    public void startDocument() throws SAXException {
        // Nothing to do
        handler.sendMessage(Message.obtain(handler, ParserXMLHandler.START_DOCUMENT));
	}
	
	@Override
    public void endDocument() throws SAXException {
          // Nothing to do
        handler.sendMessage(Message.obtain(handler, ParserXMLHandler.END_DOCUMENT));
//        if( mActivity != null )
//        	mActivity.didFinishParsing();
    }
	
	/** Called when tag starts ( ex:- <name>AndroidPeople</name>
	* -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		
		currentElement = true;
        
		ElementData element = new ElementData();
        
        element.name = localName;
        for( int i = 0; i < attributes.getLength(); i++ ) {
        	String aLocalName = attributes.getLocalName(i);
        	String aValue = attributes.getValue(aLocalName);
        	element.attributes.put( aLocalName, aValue );
			Log.d( "chud", "Type :" + aLocalName + " Value :" + aValue );
        }

        handler.sendMessage( Message.obtain( handler, ParserXMLHandler.DID_START_ELEMENT, element ) );
        
	}

	/** Called when tag closing ( ex:- <name>AndroidPeople</name>
	* -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		
		currentElement = false;
        
		ElementData element = new ElementData();
        
        element.name = localName;
        element.value = currentValue;
        handler.sendMessage( Message.obtain( handler, ParserXMLHandler.DID_FINISH_ELEMENT, element ) );
        currentValue = "";
        
	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
	* -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}
	}
}
